const service=require('../service/label.service')

const verifyLabelExists =async (ctx,next)=>{
  const {labels}=ctx.request.body
  const newLabels=[]

  // 遍历传过来的标签数组
  for(let name of labels){
    const label={name}

    const labelResult=await service.getLabelByName(name)
    // 标签不存在则在label表创建这个标签
    if(!labelResult){
      const result=await service.create(name)
      label.id=result.insertId
    }else{
      label.id=labelResult.id
    }

    newLabels.push(label)
  }
  ctx.labels=newLabels
  
  await next()
}

module.exports={
  verifyLabelExists
}

